package com.norma.netty.study.netty.disruptor;

import com.lmax.disruptor.BlockingWaitStrategy;
import com.lmax.disruptor.dsl.ProducerType;
import com.norma.netty.study.netty.disruptor.core.RingBufferWorkerPoolFactory;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.util.ArrayList;

/**
 * @author zhangchaofeng
 * @date 2021/1/5
 * @description
 */
public class NettyClient {

    private static final String HOST = "127.0.0.1";
    private static final int PORT = 9998;

    public static void main(String[] args) {
        //init and start disruptor
        // bufferSize is best to a power of 2
        ArrayList<MessageConsumerImpl4Client> messageConsumerImpl4Clients = new ArrayList<>();
        messageConsumerImpl4Clients.add(new MessageConsumerImpl4Client("1"));
        messageConsumerImpl4Clients.add(new MessageConsumerImpl4Client("2"));

        RingBufferWorkerPoolFactory.getInstance().initAndStart(ProducerType.MULTI,
                1024,
                new BlockingWaitStrategy(), messageConsumerImpl4Clients
                        .toArray(new MessageConsumerImpl4Client[messageConsumerImpl4Clients.size()]));
        //start netty server
        NioEventLoopGroup executors = new NioEventLoopGroup();
        Bootstrap bootstrap;
        try {
            bootstrap = new Bootstrap()
                    .group(executors)
                    .channel(NioSocketChannel.class)
                    //缓冲区动态调配
                    .option(ChannelOption.RCVBUF_ALLOCATOR, AdaptiveRecvByteBufAllocator.DEFAULT)
                    //缓冲区 池化操作
                    .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            ChannelPipeline pipeline = ch.pipeline();
                            pipeline.addLast(MarshallingCodeCFactory.buildMarshallingEncoder());
                            pipeline.addLast(MarshallingCodeCFactory.buildMarshallingDecoder());
                            pipeline.addLast(new ClientHandler());
                        }
                    });
            ChannelFuture connect = bootstrap.connect(HOST, PORT).sync();
            Channel channel = connect.channel();
            sendData(channel);
            connect.channel().closeFuture().sync();
        } catch (Exception e) {

        } finally {
            executors.shutdownGracefully();
        }
    }

    private static void sendData(Channel channel) {
        for (int i = 0; i < 10; i++) {
            TransData data = new TransData(String.valueOf(i),
                    "发送数据" + i, String.valueOf(i));
            channel.writeAndFlush(data);
        }
    }
}
